package com.itheima.tlias84.controller;


import com.itheima.tlias84.dto.SelectAllEmpDTO;
import com.itheima.tlias84.dto.SelectEmpDTO;
import com.itheima.tlias84.dto.SelectEmpDetailDTO;
import com.itheima.tlias84.entity.Emp;
import com.itheima.tlias84.po.InsertEmpParam;
import com.itheima.tlias84.po.SelectEmpPageParam;
import com.itheima.tlias84.po.UpdateEmpParam;
import com.itheima.tlias84.service.EmpService;
import com.itheima.tlias84.vo.PageVO;
import com.itheima.tlias84.utils.ResultUtil;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

@Slf4j
@RestController
@RequestMapping("emps")
public class EmpController {
    @Resource
    EmpService empService;

    @GetMapping
    public ResultUtil selectEmpByPage(SelectEmpPageParam param){
        if (Objects.isNull(param.getPage()) || Objects.isNull(param.getPageSize())){
            return ResultUtil.fail("分页参数不存在");
        }
        return empService.selectEmpByPage(param);
    }

//    @PostMapping
//    public ResultUtil insertOne(@RequestBody Emp emp){
//        int i = empService.insertOne(emp);
//        return ResultUtil.success();
//    }


    @DeleteMapping
    public ResultUtil batchDelete(@RequestParam("ids") List<Integer> ids){
        if (ids.isEmpty()){
            return ResultUtil.fail("请先选择的id");
        }
        int i = empService.batchDelete(ids);
        return ResultUtil.success(i);
    }

    @PostMapping
    public ResultUtil insertEmp(@Validated  @RequestBody InsertEmpParam param){
//        if(Objects.isNull(param.getUsername())){
//            return ResultUtil.fail("用户名不能为空");
//        }

        int i = empService.insertEmp(param);
        return i == 0 ? ResultUtil.fail("失败") :ResultUtil.success("成功");
    }

    @GetMapping("/{id}")
    public ResultUtil selectEmpById(@PathVariable("id") Integer id){
        SelectEmpDetailDTO selectEmpDetailDTO = empService.selectEmpByid(id);
        return ResultUtil.success(selectEmpDetailDTO);
    }

    @PutMapping
    public ResultUtil updateEmp(@RequestBody UpdateEmpParam param){
        int i = empService.updateEmp(param);
        return i == 0 ? ResultUtil.fail("失败") :ResultUtil.success("成功");
    }

    @GetMapping("/list")
    public ResultUtil selectAllEmp(){
        List<SelectAllEmpDTO> selectAllEmpDTOS = empService.selectAllEmp();
        return ResultUtil.success(selectAllEmpDTOS);
    }
}
